﻿using MyToDo.Common.Models;
using MyToDo.Extensions;
using MyToDo.Service.Login;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyToDo.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        private readonly ILoginService loginService;
        private readonly IEventAggregator aggregator;

        public string Title { get; set; } = "TODO";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }

        public LoginViewModel(ILoginService loginService,IEventAggregator aggregator)
        {
            UserDto = new ResgiterUserDto();
            ExecuteCommand = new DelegateCommand<string>(Execute);
            this.loginService = loginService;
            this.aggregator = aggregator;
        }

        private void Execute(string obj)
        {
            switch (obj) {
                case "Login":Login();break;
                case "LoginOut": LoginOut(); break;
                case "Go": SelectedIndex = 1; break;
                case "Resgister": Resgister(); break;
                case "Return": SelectedIndex = 0; break;
            }
        }
        async void Login() {
            if (string.IsNullOrWhiteSpace(Account) || string.IsNullOrWhiteSpace(PassWord)) {
                return;
            }
            var loginResult = await loginService.LoginAsync(new Common.Models.UserDto()
            {
                Account = Account,
                PassWord = PassWord
            });
            if (loginResult.Status) {
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
            aggregator.SendMessage(loginResult.Message, "Login");
        }
        void LoginOut() {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));
        }
        async void Resgister() {
            var resgiterResult = await loginService.ResgiterAsync(new Common.Models.UserDto()
            {
                Account = UserDto.Account,
                UserName = UserDto.UserName,
                PassWord = UserDto.PassWord,
            });
            if (resgiterResult != null && resgiterResult.Status) {
                SelectedIndex = 0;
            }
        }
        public DelegateCommand<string> ExecuteCommand { get; set; }
        private string account;
        public string Account
        {
            get { return account; }
            set { account = value; RaisePropertyChanged(); }
        }
        private string passWord;        

        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }
        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; RaisePropertyChanged(); }
        }
        private ResgiterUserDto userDto;02
        public ResgiterUserDto UserDto
        {
            get { return userDto; }
            set {  userDto = value; RaisePropertyChanged(); }
        }
    }
}
